﻿using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.BLL
{
    public class NurseTransferManager : BaseManager
    {
        /// <summary>
        /// 添加/编辑转科申请
        /// </summary>
        /// <param name="nurseTransfer"></param>
        /// <returns></returns>
        public static OperateResult Save(NurseTransfer nurseTransfer)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                SysLoginUser loginUser = LoginManager.GetLoginUser();

                if (nurseTransfer.Id == 0)
                {
                    nurseTransfer.CreateTime = DateTime.Now;
                    nurseTransfer.CreateUserId = loginUser.Personnel_id;
                }
                else
                {
                    nurseTransfer.UpdateTime = DateTime.Now;
                    nurseTransfer.UpdateUserId = loginUser.Personnel_id;
                }
                nurseTransfer.UserId = loginUser.Personnel_id;
                nurseTransfer.DepartmentCode = loginUser.CurrDepCode;

                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfP_NurseTransfer result = service.setP_NurseTransfer(nurseTransfer);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取转科申请详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static OperateResult GetNurseTransferInfo(string id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfNurseTransfer result = service.getP_NurseTransfer("", id);
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <param name="reviewReason"></param>
        /// <returns></returns>
        public static OperateResult Review(int id, int status, string reviewReason)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                Loginuser loginUser = LoginManager.GetLoginUser();
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfNurseTransfer result = service.getP_NurseTransfer("", id.ToString());

                if (result.S)
                {
                    NurseTransfer nurseTransfer = result.D;
                    if (nurseTransfer.Status != 0)
                    {
                        resultInfo.Result = 0;
                    }

                    if (resultInfo.Result == 1)
                    {
                        ResultHelperOfBoolean result1 = service.auditP_NurseTransfer(status, loginUser.Personnel_id, reviewReason, DateTime.Now, id);
                        if (!result1.S)
                        {
                            resultInfo.Result = 0;
                            resultInfo.Body = result1.M;
                        }
                    }
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}
